package threading;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2023-03-26
 * Time: 15:42
 */
public class ThreadExample_Interrupted2 {
    public static void main(String[] args) {
        Thread t = new Thread(()->{
            // currentThread 是获取到当前线程实例
            // 此处 currentThread 得到的对象就是 t

            // 经过验证，这里的条件改为true效果是一样的
            while (!Thread.currentThread().isInterrupted()) {
                System.out.println("hello t");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    // 由于这里sleep的特殊机制，这里需要加上break;
//                    break;
                }
            }
        });

        t.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 将内部的标志位设置成true
        t.interrupt();
    }
}
